package com.htdu87.vnlicenceplatedetection;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class ViewFinder extends View {
	private int recWith;
	private int recHeight;
	private Rect rectFinder;
	private Paint mPaint;
	private Point screenSize;
	
	public ViewFinder(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		recHeight = 150;
		recWith = 150;
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		screenSize = new Point();
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		display.getSize(screenSize);
		
		genRect();
	}

	private void genRect(){
		rectFinder = new Rect((screenSize.x/2)-(recWith/2), (screenSize.y/2)-(recHeight/2), 
				(screenSize.x/2)+(recWith/2), (screenSize.y/2)+(recHeight/2));
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		mPaint.setColor(0xffffffff);
		mPaint.setStyle(Paint.Style.STROKE);
		canvas.drawRect(rectFinder, mPaint);
		
		mPaint.setColor(0x60000000);
		mPaint.setStyle(Paint.Style.FILL);
		canvas.drawRect(0, 0, screenSize.x, rectFinder.top, mPaint);
		canvas.drawRect(0, rectFinder.bottom, screenSize.x, screenSize.y, mPaint);
		canvas.drawRect(0, rectFinder.top, rectFinder.left, rectFinder.bottom, mPaint);
		canvas.drawRect(rectFinder.right, rectFinder.top, screenSize.x, rectFinder.bottom, mPaint);
		
		mPaint.setColor(0xffffffff);
		canvas.drawRect(rectFinder.left - 10, rectFinder.top - 10, rectFinder.left + 10, rectFinder.top + 10, mPaint);
		canvas.drawRect(rectFinder.right - 10, rectFinder.top - 10, rectFinder.right + 10, rectFinder.top + 10, mPaint);
		canvas.drawRect(rectFinder.left - 10, rectFinder.bottom - 10, rectFinder.left + 10, rectFinder.bottom + 10, mPaint);
		canvas.drawRect(rectFinder.right - 10, rectFinder.bottom - 10, rectFinder.right + 10, rectFinder.bottom + 10, mPaint);
	}

	public int getRectWidth() {
		return recWith;
	}

	public void setRectWidth(int recWith) {
		this.recWith = recWith;
		genRect();
	}

	public int getRectHeight() {
		return recHeight;
	}

	public void setRectHeight(int recHeight) {
		this.recHeight = recHeight;
		genRect();
	}
	
	public Rect getRectFinder(){
		return rectFinder;
	}
	
	public void refreshView(){
		this.invalidate();
	}
}
